package org.dfzt.modules.online.cgform.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import org.dfzt.common.api.vo.Result;
import org.dfzt.common.system.query.QueryGenerator;
import org.dfzt.modules.online.cgform.entity.OnlCgformButton;
import org.dfzt.modules.online.cgform.service.IOnlCgformButtonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController("onlCgformButtonController")
@RequestMapping({"/online/cgform/button"})
public class OnlCgformButtonController {
    private static final Logger a = LoggerFactory.getLogger(OnlCgformButtonController.class);


    @Autowired
    private IOnlCgformButtonService onlCgformButtonService;


    @GetMapping({"/list/{code}"})
    public Result<IPage<OnlCgformButton>> a(OnlCgformButton paramOnlCgformButton, @RequestParam(name = "pageNo", defaultValue = "1") Integer paramInteger1, @RequestParam(name = "pageSize", defaultValue = "10") Integer paramInteger2, HttpServletRequest paramHttpServletRequest, @PathVariable("code") String paramString) {
        Result result = new Result();
        paramOnlCgformButton.setCgformHeadId(paramString);
        QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(paramOnlCgformButton, paramHttpServletRequest.getParameterMap());
        Page page = new Page(paramInteger1.intValue(), paramInteger2.intValue());
        IPage iPage = this.onlCgformButtonService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(iPage);
        return result;
    }


    @PostMapping({"/add"})
    public Result<OnlCgformButton> a(@RequestBody OnlCgformButton paramOnlCgformButton) {
        Result result = new Result();
        try {
            this.onlCgformButtonService.save(paramOnlCgformButton);
            result.success("添加成功！");
        } catch (Exception exception) {
            a.error(exception.getMessage(), exception);
            result.error500("操作失败");
        }
        return result;
    }


    @PutMapping({"/edit"})
    public Result<OnlCgformButton> b(@RequestBody OnlCgformButton paramOnlCgformButton) {
        Result result = new Result();
        OnlCgformButton onlCgformButton = (OnlCgformButton) this.onlCgformButtonService.getById(paramOnlCgformButton.getId());
        if (onlCgformButton == null) {
            result.error500("未找到对应实体");
        } else {
            boolean bool = this.onlCgformButtonService.updateById(paramOnlCgformButton);

            if (bool) {
                result.success("修改成功!");
            }
        }

        return result;
    }


    @DeleteMapping({"/delete"})
    public Result<OnlCgformButton> a(@RequestParam(name = "id", required = true) String paramString) {
        Result result = new Result();
        OnlCgformButton onlCgformButton = (OnlCgformButton) this.onlCgformButtonService.getById(paramString);
        if (onlCgformButton == null) {
            result.error500("未找到对应实体");
        } else {
            boolean bool = this.onlCgformButtonService.removeById(paramString);
            if (bool) {
                result.success("删除成功!");
            }
        }

        return result;
    }


    @DeleteMapping({"/deleteBatch"})
    public Result<OnlCgformButton> b(@RequestParam(name = "ids", required = true) String paramString) {
        Result result = new Result();
        if (paramString == null || "".equals(paramString.trim())) {
            result.error500("参数不识别！");
        } else {
            this.onlCgformButtonService.removeByIds(Arrays.asList(paramString.split(",")));
            result.success("删除成功!");
        }
        return result;
    }


    @GetMapping({"/queryById"})
    public Result<OnlCgformButton> c(@RequestParam(name = "id", required = true) String paramString) {
        Result result = new Result();
        OnlCgformButton onlCgformButton = (OnlCgformButton) this.onlCgformButtonService.getById(paramString);
        if (onlCgformButton == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(onlCgformButton);
            result.setSuccess(true);
        }
        return result;
    }
}
